package com.greenleaf.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenleaf.entity.PdtImgs;
import com.greenleaf.entity.PdtSize;
import com.greenleaf.entity.Product;
import com.greenleaf.entity.SearchCon;
import com.greenleaf.service.PdtImgService;
import com.greenleaf.service.PdtSizeService;
import com.greenleaf.service.ProductService;

@Controller
@RequestMapping("/product")
public class PropuctController {
	@Resource
	private ProductService productService;
	@Resource
	private PdtSizeService pdtSizeService;
	@Resource
	private PdtImgService pdtImgService;
	@RequestMapping("/getAll")
	@ResponseBody
	//http://localhost:8080/Greenleaf/product/getAll
	public Object getAllProduct(SearchCon searchCon,HttpServletRequest request) {
		List<Product> user = productService.getAllProduct(searchCon);
		HttpSession session=request.getSession();
		int i=1;
		for (Product product : user) {
			String j="product"+i;
			System.out.print(j+"--------");
			System.out.println(product);
			session.setAttribute(j, product);
			session.setMaxInactiveInterval(60*60);//单位为秒
			i++;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "0");
		map.put("msg", "0");
		map.put("count",productService.getTotalProduct(searchCon));
		map.put("data", user);
		return map;
	}

	
	@RequestMapping("/getby")
	@ResponseBody // http://localhost:8080/Greenleaf/product/getby
	public Object getProductById(Integer pdtId) {
		Product user = productService.getProductById(pdtId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "0");
		map.put("msg", "0");
		map.put("count", 1);
		map.put("data", user);
		return map;
	}

	@RequestMapping("/add")
	@ResponseBody
	//http://localhost:8080/Greenleaf/product/add
	public Object addProduct(Product product) {
		int r=productService.addProduct(product);//添加产品
		String name = "";
		//添加库存
		for (int i = 0; i < 6; i++) {
			if(i==0) {name="一岁龄";}
			else if (i==1) {name="二岁龄";}
			else if (i==2) {name="三岁龄";}
			else if (i==3) {name="四岁龄";}
			else if (i==4) {name="五岁龄";}
			else if (i==5) {name="六岁龄";}
			PdtSize pdtSize = new PdtSize(null, name, product.getPdtId() , null);
			pdtSizeService.addPdtSize(pdtSize);
		}
		//添加图片
		for (int i = 1; i <= 4; i++) {
			PdtImgs pdtImg = new PdtImgs(null,product.getPdtId() , "images/img404.png", i);
			pdtImgService.addPdtImg(pdtImg);
		}
		return  r==1?true:false;
	}
	@RequestMapping("/update")
	@ResponseBody
	//http://localhost:8080/Greenleaf/product/update
	public Object updateProduct(Product product) {
		//System.out.println(product);
		int r = productService.updateProduct(product);
		return r!=0?true:false;
	}
	@RequestMapping("/updateBySwitch")
	@ResponseBody
	//http://localhost:8080/Greenleaf/product/updateBySwitch
	public Object updateProductBySwitch(Boolean checked ,int pdtId) {
		Product product = new Product(pdtId, null, 0 , null, null, null, null, checked==true?"是":"否", null, null);
		//System.out.println(product);
		int r = productService.updateProductBySwitch(product);
		return r!=0?true:false;
	}
	@RequestMapping("/del")
	@ResponseBody
	//http://localhost:8080/Greenleaf/product/del
	public Object delProduct(int pdtId) {
		//System.out.println(pdtId);
		pdtImgService.delPdtImgs(pdtId);//删除产品图片
		pdtSizeService.delPdtSize(pdtId);//删除库存
		int r =  productService.delProduct(pdtId);
		return  r==1?true:false;
	}
	@RequestMapping("/dels")
	@ResponseBody
	//http://localhost:8080/Greenleaf/product/dels
	public Object delProducts(String ids) {
		String[] listIds = ids.split(",");
		List<Integer> list = new ArrayList<Integer>();
		for (String listStr : listIds) {
			pdtSizeService.delPdtSize(Integer.parseInt(listStr));
			pdtImgService.delPdtImgs(Integer.parseInt(listStr));//删除产品图片
			list.add(Integer.parseInt(listStr));
		}
	
		int r = productService.delProducts(list);
		return r!=0?true:false;
	}
	
}
